﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Oplaco.Core.DAL;
using Oplaco.Core.BusinessLogicLayer;
using System.Data;
using System.Collections;

namespace Oplaco.Web.DTO
{
    public class CachedLocationCollection : ICollection<CachedLocation>
    {
        
        private List<CachedLocation> innerCollection;
        private bool isRO;

        #region Construct

        /// <summary>
        /// Custom constructor
        /// </summary>
        /// <param name="context"></param>
       
        #endregion

        /// <summary>
        /// Adds an index to the collection.
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public CachedLocation this[int index]
        {
            get { return (CachedLocation)innerCollection[index]; }
            set { innerCollection[index] = value; }
        }

        /// <summary>
        /// Adds item to collection
        /// </summary>
        /// <param name="item"></param>
        public void Add(CachedLocation item)
        {
            if (!Contains(item))
            {
                innerCollection.Add(item);
            }
            else {
                Oplaco.Exceptions.AppService.ProcessAndNotifyDataAccessLayerException(new Exception("Collection alreaady containts the item:" + item.Id));
            }
        }

        /// <summary>
        /// Clears the collection
        /// </summary>
        public void Clear()
        {
            innerCollection.Clear();
        }

        /// <summary>
        /// Check if item is part of the collection
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Contains(CachedLocation item)
        {
            CachedLocation found = (from l in innerCollection where l.Equals(item) select l).Single<CachedLocation>();
            if (found == null) return false;
            return true;
        }

        /// <summary>
        /// Copies array to collection
        /// </summary>
        /// <param name="array"></param>
        /// <param name="arrayIndex"></param>
        public void CopyTo(CachedLocation[] array, int arrayIndex)
        {
            innerCollection = (from item in array select item).ToList<CachedLocation>();
        }

        /// <summary>
        /// The mount of items in the collection
        /// </summary>
        public int Count
        {
            get { return innerCollection.Count; }
        }

        /// <summary>
        /// Check for readonly collection
        /// </summary>
        public bool IsReadOnly
        {
            get { return isRO; }
        }

        /// <summary>
        /// Removes item from collection
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Remove(CachedLocation item)
        {
            CachedLocation found = (from l in innerCollection where l.Equals(item) select l).Single<CachedLocation>();
            if (found==null) return false;
            innerCollection.Remove(item);
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerator<CachedLocation> GetEnumerator()
        {
            return new CachedLocationEnumerator(this);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return new CachedLocationEnumerator(this);
        }

        
    }



    /// <summary>
    /// Defines the enumerator for the Boxes collection.
    /// (Some prefer this class outside the collection class.)
    /// </summary>
    public class CachedLocationEnumerator : IEnumerator<CachedLocation>
    {
        private CachedLocationCollection _collection;
        private int curIndex;
        private CachedLocation curItem;

        /// <summary>
        /// Construct
        /// </summary>
        /// <param name="collection"></param>
        public CachedLocationEnumerator(CachedLocationCollection collection)
        {
            _collection = collection;
            curIndex = -1;
            curItem = default(CachedLocation);

        }

        /// <summary>
        /// Moves to the next item in the collection
        /// </summary>
        /// <returns></returns>
        public bool MoveNext()
        {
            //Avoids going beyond the end of the collection.
            if (++curIndex >= _collection.Count)
            {
                return false;
            }
            else
            {
                // Set current box to next item in collection.
                curItem = _collection[curIndex];
            }
            return true;
        }

        /// <summary>
        /// Resets the collection index
        /// </summary>
        public void Reset() { curIndex = -1; }

        void IDisposable.Dispose() { }

        /// <summary>
        /// Fetchs the current item from the collection
        /// </summary>
        public CachedLocation Current
        {
            get { return curItem; }
        }

        /// <summary>
        /// Fetchs the current item from the collection
        /// </summary>
        object IEnumerator.Current
        {
            get { return Current; }
        }

    }



}
